package com.mpp.hosp.controller.api;

import com.mpp.common.result.Result;
import com.mpp.common.util.StringUtils;
import com.mpp.model.entity.hosp.Department;
import com.mpp.model.entity.hosp.Hospital;
import com.mpp.model.entity.hosp.Schedule;
import com.mpp.model.vo.hosp.DepartmentQueryVo;
import com.mpp.model.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author MMDZ
 * @date 2022/8/17 23:03
 * @description TODO
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController extends BaseController {

    // 上传医院接口- mongodb
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        // 校验签名步骤
        Map<String, Object> paramMap = CheckSignStep(request);
        // 传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);
        // 调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }

    // 查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        // 校验签名步骤
        Map<String, Object> paramMap = CheckSignStep(request);
        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(paramMap.get("hoscode").toString());
        return Result.ok(hospital);
    }

    // 上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 校验签名步骤
        Map<String, Object> paramMap = CheckSignStep(request);
        //调用service的方法
        departmentService.save(paramMap);
        return Result.ok();
    }

    // 查询科室接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        // 校验签名步骤  TODO 签名校验
        Map<String, Object> paramMap = CheckSignStep(request);
        // 医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 当前页和每页记录数
        int page = StringUtils.isNull(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isNull(paramMap.get("limit")) ? 5 : Integer.parseInt((String) paramMap.get("limit"));

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        // 校验签名步骤  TODO 签名校验
        Map<String, Object> paramMap = CheckSignStep(request);
        // 医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 校验签名步骤  TODO 签名校验
        Map<String, Object> paramMap = CheckSignStep(request);
        scheduleService.save(paramMap);
        return Result.ok();
    }

    //查询排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        // 校验签名步骤  TODO 签名校验
        Map<String, Object> paramMap = CheckSignStep(request);
        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //科室编号
        String depcode = (String) paramMap.get("depcode");
        //当前页 和 每页记录数
        int page = StringUtils.isNull(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isNull(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //删除排班
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request) {
        // 校验签名步骤  TODO 签名校验
        Map<String, Object> paramMap = CheckSignStep(request);
        // 获取医院编号和排班编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }
}
